var table = layui.table;
var laydate = layui.laydate;
var form = layui.form;
$(document).ready(function() {
    //初始化数据列表
    load();

    //初始化右边工具栏
    initRightToolbar();

    //加载分类
    $.ajax({
        url: '/my/info/find_info_classify',
        dataType: 'json',
        data:{claType:0},
        type: 'get',
        async: false,
        success: function (result) {
            $.each(result.data,function (index, item) {
                //排除友情链接和资料下载
                if(item.id==16||item.id==18){
                    return;
                }
                var option = new Option(item.claName, item.id);
                $('#conType').append(option);// 下拉菜单里添加元素
            });
            form.render("select");
            //重新渲染 固定写法
        }
    });

    form.on('select(conType)', function(data) {
        var id = data.value;
        $('#search_res_type').html("<option value=\"\">请选择</option>");
        $.ajax({
            url: '/my/info/find_info_classify',
            dataType: 'json',
            data:{pid:id,claType:1},
            type: 'get',
            async: false,
            success: function(result) {
                $.each(result.data,function (index, item) {
                    var option = new Option(item.claName, item.id);
                    $('#search_res_type').append(option);// 下拉菜单里添加元素
                });
                form.render("select");
            }
        });
    });

});

// 渲染时间选择框
laydate.render({
    elem: '#beginDate'
    ,format: 'yyyy-MM-dd'
});
laydate.render({
    elem: '#endDate'
    ,format: 'yyyy-MM-dd'
});

laydate.render({
    elem: '#publishDate'
    ,format: 'yyyy-MM-dd'
});

function load() {
    table.render({
        id: 'allInfoTable',
        elem: '#allInfoTable',
        method: 'post',
        url:'/all/info/find_info_list',
        response: {
            statusCode: 200
        },
        cols: [[
            {field:'title', title: '信息标题', sort: true,/*templet:function(row){
                    return '<a href="/api/user/center_content.html?pid='+row.parentClassify.id+'&id='+row.childClassify.id+'&contId='+row.id+'">'+row.title+'</a>';
            }*/},
            {field:'parentClassify.claName', title: '信息类型',sort: true,templet:function(row){
                    return row.parentClassify.claName;
                }},
            {field:'childClassify.claName', title: '信息小类', sort: true,templet:function(row){
                    return row.childClassify.claName;
                }},
            {field:'beginDate', title: '有效期开始', sort: true},
            {field:'endDate', title: '有效期结束', sort: true},
            {field:'createName', title: '发布人', sort: true},
            {field:'depName', title: '发布科室', sort: true,templet:function(row){
                    return row.dept.depName;
                }},
            {field:'publishDate', title: '发布时间', sort: true},
            {fixed: 'right', width: 240, align: 'center', unresize: true, toolbar: '#rightToolbar'}
        ]],
        page: true
    });

    $("#searchBtn").click(search);
}

/**
 * 预览
 * @param obj
 */
/*function preview(row) {
    var index=layer.open({
        type: 2,
        title: '新闻预览',
        fix: false,
        resize: false,
        btn: ['关闭'],
        content: '/api/user/center_content.html?pid='+row.data.parentClassify.id+'&id='+row.data.childClassify.id+'&contId='+row.data.id
    });
    layer.full(index);
}*/

function initRightToolbar() {
    table.on('tool(allInfoTable)', function(obj){
        if(obj.event === 'allInfo_detail') {
            allInfoDetail(obj);
        }
        if(obj.event === 'allInfo_del') {
            allInfoDel(obj);
        }
        if(obj.event === 'preview') {
            preview(obj);
        }
    });
}

/**
 * 详情按钮
 * @param obj
 */
function allInfoDetail(obj) {
    var index = layer.open({
        type: 2,
        title: '详情',
        fix: false,
        resize: false,
        area: ['50%', '80%'],
        btn: ['关闭'],
        content: '/all/info/toDetail/' + obj.data.id,
    });
    layer.full(index);
}

/**
 * 删除按钮
 * @param obj
 */
function allInfoDel(obj) {
    layer.confirm('确定删除数据吗？', function(index){
        $.get("/all/info/del/" + obj.data.id, function(result) {
            if (200 == result.code) {
                layer.close(index);
                table.reload('allInfoTable');
            } else {
                layer.alert("删除失败");
            }
        });
    });
}

/**
 * 查询按钮
 */
function search() {
    table.reload('allInfoTable', {
        page: {
            curr: 1
        },
        where: $("#forms").serializeObject()
    });
}

